<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
	<script>
		//扁平话数据结构变成树状数组
		let myarr = [
			{ id: 1, name: '部门1', pid: 0 },
			{ id: 2, name: '部门2', pid: 1 },
			{ id: 3, name: '部门3', pid: 1 },
			{ id: 4, name: '部门4', pid: 3 },
			{ id: 5, name: '部门5', pid: 4 },
		]
		myarr.forEach((item, idx, arr) => {
			if (idx < myarr.length - 1) {
				item.children = arr[idx + 1]
			}
		})
		console.log(myarr[0])

	</script>
</body>

</html>